//
//  YDMyCardViewController.m
//  IngCard
//
//  Created by kf on 2017/8/11.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDMyCardViewController.h"
#import "YDMyCardHeaderView.h"
#import "YDMyCardTableViewCell.h"
#import "YDActivityManager.h"
#import "YDNewContactsViewController.h"
#import "YDBTMessageManager.h"
#import "YDTextViewKeyboardViewController.h"
@interface YDMyCardViewController ()<UINavigationControllerDelegate>
@property(nonatomic, strong) YDTextViewKeyboardViewController *textViewKeyboardViewController;

@end

@implementation YDMyCardViewController

- (void) viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.delegate = self;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    YDMyCardHeaderView *headerView = [YDMyCardHeaderView new];
    headerView.sync = ^{
        
        
        [BTMESSAGE run];
        
        Weak(self);
        [ACTIVITY sync:nil OnComplete:^(BOOL success, NSString *msg,
                                        NSMutableArray<YDActivity *> *syncActivity,
                                        NSMutableArray<YDContact *> *syncContacts)
         {
            if (syncActivity && (syncActivity.count > 0))
            {
                [syncActivity enumerateObjectsUsingBlock:^(YDActivity * _Nonnull activity, NSUInteger idx, BOOL * _Nonnull stop) {

                    [ACTIVITY.activities insertObject:activity atIndex:idx];
                    [self.dataSource insertDataSource:[YDMyCardCellDataSource class] atSection:0
                                               config:^(YDMyCardCellDataSource* dataSource)
                     {
                         dataSource.activity = activity;
                         Weak(dataSource);
                         dataSource.edit = ^{
                             Strong(self); Strong(dataSource);
                             [self editContent:dataSource];
                         };
                     }];
                }];
            }

             if(syncContacts && (syncContacts.count > 0))
             {
                 YDNewContactsViewController *vc = [YDNewContactsViewController new];

                 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];

                 [self.navigationController presentViewController:nc animated:YES completion:^{

                 }];
             }
        }];
    };
    headerView.frame = CGRectMake(0, 0, 0, 461);
    self.tableView.tableHeaderView = headerView;
    
    UIEdgeInsets edge = UIEdgeInsetsMake(-20, 0, 0, 0);
    [self.tableView setContentInset:edge];
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (ACTIVITY.beAdded)
    {
        Weak(self);
        [self.dataSource addDataSource:[YDMyCardCellDataSource class] atSection:0
                                config:^(YDMyCardCellDataSource* dataSource)
        {
            dataSource.activity = [[YDActivity alloc] init];
            dataSource.activity.type = YDActivity_contact;
            dataSource.activity.contacts = ACTIVITY.addContacts;
            
            Weak(dataSource);
            dataSource.edit = ^{
                Strong(self); Strong(dataSource);
                [self editContent:dataSource];
            };
        }];
        
        ACTIVITY.beAdded = NO;
    }
    
}


- (void) initDataSource
{
    Weak(self);
    [ACTIVITY getMoreOnComplete:^(BOOL success, NSString *msg) {
       
        [ACTIVITY.activities enumerateObjectsUsingBlock:^(YDActivity * _Nonnull activity, NSUInteger idx, BOOL * _Nonnull stop)
         {
             [self.dataSource addDataSource:[YDMyCardCellDataSource class] atSection:0 config:^(YDMyCardCellDataSource* dataSource) {
                 dataSource.activity = activity;
                 
                 Weak(dataSource);
                 dataSource.edit = ^{
                     Strong(self); Strong(dataSource);
                     [self editContent:dataSource];
                 };
             }];
        }];
    }];
}
#pragma mark - Private
-(void) editContent:(YDMyCardCellDataSource*) cellModel
{
    NSString *placehold = cellModel.activity.content ?: @"请输出备注";
    
    [self.textViewKeyboardViewController showWithPlaceholder:placehold onComplete:^(NSString *value)
     {
         cellModel.activity.content = value;
         [self.dataSource reloadDataSource:cellModel];
         [self.tableView layoutIfNeeded];
     } onCancel:^
     {
         Log(@"cancel");
     }];
    
}


// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];    
}

#pragma mark - lazy load
-(YDTextViewKeyboardViewController *)textViewKeyboardViewController
{
    if (!_textViewKeyboardViewController)
    {
        _textViewKeyboardViewController = [[YDTextViewKeyboardViewController alloc] init];
        _textViewKeyboardViewController.parendViewController = self;
    }
    return _textViewKeyboardViewController;
}

@end
